// pages/appLetWork/work9/index.js
var arrayincp = [{ text: [], money: [], time: [] }];//收入
var arraycp = [{ text: [], money: [], time: [] }];//支出
import Toast from '@vant/weapp/toast/toast';

Page({
  data: {
    addDialogShow: false,
    addValue: "",//收入输入框的值
    add_eventValue: "",
    expendDialogShow: false,
    expendValue: "",//支出输入框的值
    expend_eventValue: "",
    //下面才是真正数据展示
    addArray: arrayincp,
    expendArray: arraycp,
    add_total: 0,//总收入
    income_summary: [],//收入汇总
    expend_summary: [],//支出汇总
    summary_date: null,
    expend_total: 0,//总支出
    balance: 0,//余额
    //日期
    addDateYear: 2021,//+年
    addDateMonth: "",//+月
    addDateDay: "",//+日
    expendDateYear: 2021,//-年
    expendDateMonth: "",//-月
    expendDateDay: "",//-日
  },
  top() {
    wx.navigateTo({
      url: '/pages/appLetWork/work4/work4',
    })
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    })
  },

  add() {
    this.setData({
      addDialogShow: true
    })
  },
  //增加收入弹出框确定事件
  addConfirm() {
    if (this.data.addValue.trim() == "" || this.data.add_eventValue == "" || this.data.addDateYear == "" || this.data.addDateMonth == "" || this.data.addDateDay == "") {
      Toast.fail('内容不能为空');
    } else if (this.data.addValue.trim() != "" && this.data.add_eventValue != "" && this.data.addDateYear != "" && this.data.addDateMonth != "" && this.data.addDateDay != "") {
      arrayincp[0].money.push(this.data.addValue.trim());
      arrayincp[0].text.push(this.data.add_eventValue);
      arrayincp[0].time.push(this.data.addDateYear + "-" + this.data.addDateMonth + "-" + this.data.addDateDay);
      this.setData({
        addArray: arrayincp,
        addValue: "",
        add_eventValue: "",
        addDateMonth: "",
        addDateDay: ""
      })
      this.onShow()
      wx.setStorageSync("addData", this.data.addArray)
    }
  },


  expend() {
    this.setData({
      expendDialogShow: true
    })
  },
  //增加支出弹出框确定事件
  expendConfirm() {
    if (this.data.expendValue.trim() == "" || this.data.expend_eventValue == "" || this.data.expendDateYear == "" || this.data.expendDateMonth == "" || this.data.expendDateDay == "") {
      Toast.fail('内容不能为空');
    } else if (this.data.expendValue.trim() != "" && this.data.expend_eventValue != "" && this.data.expendDateYear != "" && this.data.expendDateMonth != "" && this.data.expendDateDay != "") {
      arraycp[0].money.push(this.data.expendValue.trim());
      arraycp[0].text.push(this.data.expend_eventValue);
      arraycp[0].time.push(this.data.expendDateYear + "-" + this.data.expendDateMonth + "-" + this.data.expendDateDay)
      this.setData({
        expendArray: arraycp,
        expendValue: "",
        expend_eventValue: "",
        expendDateMonth: "",
        expendDateDay: ""
      })
      this.onShow()
      wx.setStorageSync("expendData", this.data.expendArray)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var addDa = wx.getStorageSync("addData")
    var expendDa = wx.getStorageSync("expendData")
    if (addDa) {
      arrayincp = addDa
      this.setData({
        addArray: addDa,
      })
    }
    if (expendDa) {
      arraycp = expendDa
      this.setData({
        expendArray: expendDa
      })
    }
  },
  clear() {
    wx.removeStorageSync('addData')
    wx.removeStorageSync('expendData')
    setTimeout(() => {
      this.setData({
        addArray: null,
        expendArray: null,
        add_total: 0,//总收入
        expend_total: 0,//总支出
        balance: 0,//余额
        summary_date: [],
        income_summary: [],
        expend_summary: []
      });
    }, 1000);
  },

  onShow: function () {

    //console.log(array_unique(this.data.addArray[0].time))//调用日期去重
    var d = array_unique(array_unique(this.data.addArray[0].time,).concat(array_unique(this.data.expendArray[0].time))).sort();
    //console.log(d)
    //dateConcat(d, this.data.addArray[0].time, this.data.addArray[0].money)调试调用时间汇总函数

    this.setData({
      add_total: total(this.data.addArray[0].money),
      expend_total: total(this.data.expendArray[0].money),
      balance: total(this.data.addArray[0].money) - total(this.data.expendArray[0].money),

      summary_date: d, //汇总时间
      income_summary: dateConcat(d, this.data.addArray[0].time, this.data.addArray[0].money),//summary调用日期与money对应的汇总函数
      expend_summary: dateConcat(d, this.data.expendArray[0].time, this.data.expendArray[0].money)
    })
    //console.log(summary(this.data.addArray[0].time, this.data.addArray[0].money))
  },

});

//日期去重
function array_unique(arr) {
  return arr.filter(function (e, i) {
    return arr.indexOf(e) === i;
  })
};

//总收入、总支出函数
function total(money) {
  var total = 0;
  for (var i = 0; i < money.length; i++) {
    total += (Number(money[i]));
  }
  return total
};

//收入和支出日期concat
function dateConcat(concat, time, money) {
  //console.log(concat)
  var count = [];
  for (var i = 0; i < concat.length; i++) {
    var number = 0;
    for (var j = 0; j < time.length; j++) {
      if (concat[i] == time[j]) {
        number += Number(money[j])
      }
    }
    count.push(number)
  }
  return (count)
}

//字符串排序 var newArr = arr.sort(function(a,b){return a.localeCompare(b,'zh-CN')})